package apii.servlet.prof;

import static apii.servlet.prof.ProfUtils.finishResponse;
import static apii.servlet.prof.ProfUtils.getSessionProf;
import static apii.servlet.prof.ProfUtils.redirectToException;
import static apii.servlet.prof.ProfUtils.redirectToLogin;
import static apii.servlet.prof.ProfUtils.startResponse;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import apii.server.ServerThings;
import apii.servlet.ServletUtils;

/**
 * Servlet implementation class ViewGroupServlet
 */
// тут k - primary key соответствующего предмета, i - primary key
// соответствующей лабы
// j - primary key соответствующей группы
// assigned by root address: /prof/subjects/subjk/labi/groupj
// assigned by web.xml address:
// /prof_view_group?subject_id=k&lab_id=i&group_id=j
@WebServlet("/ViewGroupServlet")
public class ViewGroupServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ViewGroupServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		try (Connection c = ServerThings.getThings().getConnection();
				PreparedStatement studsStatement = c
						.prepareStatement("select user_id, surname, name, patronimic from users, students "
								+ "where users.user_id = students.student_id "
								+ "and students.group_id = ?");) {
			ProfData profData = getSessionProf(c, request);
			if (profData == null) {
				redirectToLogin(response);
				return;
			}
			int discId = (Integer) request
					.getAttribute(ProfRootServlet.DISCIPLINE_ID);
			int labId = (Integer) request.getAttribute(ProfRootServlet.LAB_ID);
			int groupId = (Integer) request
					.getAttribute(ProfRootServlet.GROUP_ID);

			studsStatement.setInt(1, groupId);

			ResultSet studsRS = studsStatement.executeQuery();
			// the response
			startResponse(response, "Группа: " + "group", profData);
			PrintWriter pw = response.getWriter();
			pw.println("	<H1>Выберите студента.</H1>");
			pw.println("	<ul>");
			while (studsRS.next()) {

				int studId = studsRS.getInt(1);
				String studName = studsRS.getString(2) + " "
						+ studsRS.getString(3) + " " + studsRS.getString(4);

				pw.println("		<li><a href=\"" + ServletUtils.PREFIX
						+ "/prof/subjects/"
						+ ServletUtils.getDisciplineURLElement(discId) + "/"
						+ ServletUtils.getLabURLElement(labId) + "/"
						+ ServletUtils.getGroupURLElement(groupId) + "/"
						+ ServletUtils.getStudURLElement(studId) + "\">"
						+ studName + "</a></li>");
			}
			pw.println("	</ul>");
			finishResponse(response);
		} catch (SQLException e) {
			e.printStackTrace();
			redirectToException(response);
		}
	}

}
